//
//  CapacityFilterViewController.m
//  OfficeBook
//
//  Created by 朱 琳 on 15/10/3.
//  Copyright (c) 2015年 sunny. All rights reserved.
//

#import "CapacityFilterViewController.h"
#import "PresentModalHelper.h"
#import "CapacityViewCell.h"
#import "TableViewHelper.h"

static NSString * const kCapacityCellIdentifer = @"CapacityCellIdentifer";

@interface CapacityFilterViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation CapacityFilterViewController

-(instancetype)init{
    self = [super init];
    if (self) {
        self.titleArray = @[@"All", @"2 ~ 4 Persons", @"4 ~ 6 Persons", @"6 ~ 8 Persons", @"8 ~ 10 Persons", @"10+"];
        self.selectedItemsIdxSet = [[NSMutableIndexSet alloc] initWithIndex:0];   //'All' as default
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ebebeb"];

    [self initNavBarItem];
    [self initTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)initNavBarItem {
    self.navigationItem.title = @"Capacity";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)];
    [self.navigationItem.leftBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(save:)];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
}

// save
-(void)save:(UIButton *)sender {
    [self.delegate viewController:self didSelectCapacity:self.selectedItemsIdxSet];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

// cancel
-(void)cancel:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)initTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[CapacityViewCell class] forCellReuseIdentifier:kCapacityCellIdentifer];
    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CapacityViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCapacityCellIdentifer];
    cell.title = self.titleArray[indexPath.row];
    if ([self.selectedItemsIdxSet containsIndex:indexPath.row]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 10.0;
    } else {
        return 0.1f;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UITableViewCell *firstCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    if (indexPath.row == 0) {  // 'All'
        [self unSelectAllItems];
        [self.selectedItemsIdxSet addIndex:0];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else{
        if ([self.selectedItemsIdxSet containsIndex:indexPath.row]) { //取消选中
            [self.selectedItemsIdxSet removeIndex:indexPath.row];
            cell.accessoryType = UITableViewCellAccessoryNone;
            //如果都没有被选中 则默认选中All
            if ([self.selectedItemsIdxSet count] == 0) {
                [self.selectedItemsIdxSet addIndex:0];
                firstCell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
        }
        else {  //选中
            [self.selectedItemsIdxSet removeIndex:0];
            firstCell.accessoryType = UITableViewCellAccessoryNone;
            [self.selectedItemsIdxSet addIndex:indexPath.row];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            // 除All外所有的都被选中 则选中All
            if ([self.selectedItemsIdxSet count] == [tableView numberOfRowsInSection:0] - 1) {
                [self unSelectAllItems];
                [self.selectedItemsIdxSet addIndex:0];
                 firstCell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
        }
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

-(void)unSelectAllItems {
    [self.selectedItemsIdxSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0]];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }];
    [self.selectedItemsIdxSet removeAllIndexes];
}

@end
